<?php
ob_clean();
header("content-type:text/html;charset=utf8");
include_once ("/Classes/PHPExcel.php");
$pdo = new PDO("mysql:host=127.0.0.1;dbname=11-9;charset=utf8","root","root");
$sql = "select * from exe";
$res = $pdo->query($sql);
$data = $res->fetchAll(PDO::FETCH_ASSOC);
//print_r($data);die;
$excel = new PHPExcel();
$sheet = $excel->setActiveSheetIndex(0);
 $sheet->setCellValue('A1','ID')
       ->setCellValue('B1','用户名')
       ->setCellValue('C1','年龄')
       ->setCellValue('D1','工资');
 $i = 2;
 foreach ($data as $k => $v)
{
    $sheet->setCellValue('A'.$i,$v['id'])
          ->setCellValue('B'.$i,$v['name'])
          ->setCellValue('C'.$i,$v['age'])
          ->setCellValue('D'.$i,$v['money']);
    $i++;
}

// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="信息.xlsx"');
header('Cache-Control: max-age=0');


$objWriter = PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
$objWriter->save('php://output');
exit;
?>